﻿Imports System.IO.File
''Imports Twitterizer
Imports TwitterVB2

Public Class frmMain
    Public tw As New TwitterAPI
    Public consumer As String = "3rZoMe2v9ZDq5T1JoyVzyw"
    Public consumersecret As String = "Za0raOo0mDs0xqgmmGxS1YI7hxUdoPmBnGtfFVHBek"
    Public oauthtoken As String
    Public oauthtokensecret As String
    Public strLogin As String
    Dim oFile As System.IO.File
    Dim oWrite As System.IO.StreamWriter
    Private IsFormBeingDragged As Boolean = False
    Private MousePos As New System.Drawing.Point(0, 0)

    Private Sub frmMain_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

        On Error Resume Next

        My.Settings.Save()
        'frmFollowers.Dispose()
        'frmFollowing.Dispose()
        'frmLogin.Dispose()
        'frmPIN.Dispose()
        'frmSpamBlocker.Dispose()
        'frmAutoTweet.Dispose()
        NotifyIcon1.Visible = False
        End

    End Sub

    Private Sub frmMain_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        'sup mang

        'frmLogin.Dispose()
        'frmPIN.Dispose()
        'frmSpamBlocker.Dispose()
        'frmAutoTweet.Dispose()
        NotifyIcon1.Visible = False

    End Sub

    Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        lblMin.Parent = PictureBox2
        lblMax.Parent = PictureBox2
        lblExit.Parent = PictureBox2

        lblMin.BackColor = Color.Transparent
        lblMax.BackColor = Color.Transparent
        lblExit.BackColor = Color.Transparent

        Try

            tw.AuthenticateWith(consumer, consumersecret, My.Settings.m_strToken, My.Settings.m_strTokenSecret)

            lblFollowers.Text = tw.AccountInformation.FollowersCount
            lblFollowing.Text = tw.AccountInformation.FriendsCount
            lblTotalTweets.Text = tw.AccountInformation.StatusesCount
            lblVerified.Text = tw.AccountInformation.Verified
            GroupBox2.Text = "@" & tw.AccountInformation.ScreenName
            PictureBox1.ImageLocation = tw.AccountInformation.ProfileImageUrl
            frmAutoTweet.PictureBox1.ImageLocation = tw.AccountInformation.ProfileImageUrl
            My.Settings.LastUsername = tw.AccountInformation.ScreenName
            My.Settings.Save()
            Me.NotifyIcon1.Visible = True

        Catch ex As Exception

            MsgBox("There was an error. Please visit www.SmartPromoter.net in the support area for help.")
            End

        End Try


        Dim FILE_NAME = Application.StartupPath & "\" & tw.AccountInformation.ScreenName.ToLower & "_ads.dat"

        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objReader1 As New System.IO.StreamReader(FILE_NAME)

            Do While objReader1.Peek() <> -1

                'lulwut

                Dim strTweet = Split(objReader1.ReadLine(), "|_SP_|")

                'frmAutoTweet.lstAutoTweet.Items.Add(strTweet(0), True)
                'frmAutoTweet.lstAT_Time.Items.Add(strTweet(1))
                'frmAutoTweet.lstAT_Uses.Items.Add(strTweet(2))

                frmAutoTweet.ListView1.BeginUpdate()
                Dim n As New ListViewItem
                n = frmAutoTweet.ListView1.Items.Add(strTweet(0))
                n.Checked = True
                n.SubItems.Add(strTweet(1))
                n.SubItems.Add("0")
                frmAutoTweet.tweetDate.Items.Add(strTweet(1))

                frmAutoTweet.ListView1.Refresh()
                frmAutoTweet.ListView1.EndUpdate()

            Loop

            objReader1.Close()

        End If

        FILE_NAME = Application.StartupPath & "\" & tw.AccountInformation.ScreenName.ToLower & "_spamblocker.dat"

        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objReader As New System.IO.StreamReader(FILE_NAME)

            Do While objReader.Peek() <> -1

                frmSpamBlocker.lstBlacklist.Items.Add(objReader.ReadLine(), True)

            Loop

            objReader.Close()

        End If

        FILE_NAME = Application.StartupPath & "\" & tw.AccountInformation.ScreenName.ToLower & "_follower-cache.dat"

        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objReader As New System.IO.StreamReader(FILE_NAME)

            Do While objReader.Peek() <> -1

                frmFollowers.lstFollowerCache.Items.Add(objReader.ReadLine())

            Loop

            objReader.Close()

            frmFollowers.Button3.Enabled = False

            If frmFollowers.lstFollowerCache.Items.Count > tw.AccountInformation.FollowersCount Then

                'we lost some followers

                Dim lostFollowers = frmFollowers.lstFollowerCache.Items.Count - tw.AccountInformation.FollowersCount

                lblPoN.Visible = True
                lblPoN.ForeColor = Color.Red
                lblPoN.Text = "↓" & lostFollowers
                BackgroundWorker1.RunWorkerAsync()

            ElseIf frmFollowers.lstFollowerCache.Items.Count < tw.AccountInformation.FollowersCount Then

                'we gained some followers

                Dim gainedFollowers = tw.AccountInformation.FollowersCount - frmFollowers.lstFollowerCache.Items.Count

                lblPoN.Visible = True
                lblPoN.ForeColor = Color.Green
                lblPoN.Text = "↑" & gainedFollowers
                BackgroundWorker1.RunWorkerAsync()

            End If

        End If

    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick

        If Me.Visible = False Then

            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            'NotifyIcon1.Visible = False

        Else

            Me.Visible = False

        End If

    End Sub

    Private Sub frmMain_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize

        If Me.WindowState = FormWindowState.Minimized Then

            Me.Visible = False

        End If

    End Sub

    Private Sub btnFollowing_Click(sender As System.Object, e As System.EventArgs) Handles btnFollowing.Click

        If frmFollowing.WindowState = FormWindowState.Minimized Then

            frmFollowing.Show()
            frmFollowing.Focus()

        Else

            frmLoading.Show()
            frmLoading.Focus()

            tmrFollowing.Enabled = True
            tmrFollowing.Start()

        End If

        'frmRoleModel.Show()
        'frmRoleModel.ShowInTaskbar = True
        'frmRoleModel.WindowState = FormWindowState.Normal

    End Sub

    Private Sub btnFollower_Click(sender As System.Object, e As System.EventArgs) Handles btnFollower.Click

        If frmFollowers.lstFollowers.Items.Count > 0 Then

            frmFollowers.Show()
            frmFollowers.Focus()

        Else

            frmLoading.Show()
            frmLoading.Focus()

            tmrFollower.Enabled = True
            tmrFollower.Start()

        End If


    End Sub

    Private Sub btnSpam_Click(sender As System.Object, e As System.EventArgs) Handles btnSpam.Click

        frmSpamBlocker.Show()
        frmSpamBlocker.ShowInTaskbar = True
        frmSpamBlocker.WindowState = FormWindowState.Normal

    End Sub

    Private Sub btnAutoTweet_Click(sender As System.Object, e As System.EventArgs) Handles btnAutoTweet.Click

        frmAutoTweet.Show()
        frmAutoTweet.ShowInTaskbar = True
        frmAutoTweet.WindowState = FormWindowState.Normal

    End Sub

    Private Sub PictureBox2_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown

        If e.Button = MouseButtons.Left Then

            IsFormBeingDragged = True

            MousePos = e.Location

        End If


    End Sub

    Private Sub PictureBox2_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove

        If IsFormBeingDragged Then

            Dim temp As Point = New Point(Me.Location + (e.Location - MousePos))
            Me.Location = temp
            temp = Nothing

        End If

    End Sub

    Private Sub PictureBox2_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseUp

        If e.Button = MouseButtons.Left Then IsFormBeingDragged = False

    End Sub

    Private Sub lblMin_Click(sender As System.Object, e As System.EventArgs) Handles lblMin.Click

        On Error Resume Next

        'If Me.WindowState = FormWindowState.Minimized Then

        Me.Visible = False

        'End If

    End Sub

    Private Sub lblExit_Click(sender As System.Object, e As System.EventArgs) Handles lblExit.Click

        On Error Resume Next

        Dim i As Integer

        'Disabled from main form.


        Dim thePath As String = Application.StartupPath & "\" & frmLogin.txtUsername.Text.ToLower & "_spamblocker.dat"
        If frmSpamBlocker.lstBlacklist.Items.Count > 0 Then
            Dim myWriter As New IO.StreamWriter(thePath)

            For Each myItem As String In frmSpamBlocker.lstBlacklist.Items
                With myItem
                    myWriter.WriteLine(myItem)

                    ' & "|_SP_|" & myItem.SubItems(1).Text
                    '& "|" & myItem.SubItems(2).Text
                End With
            Next
            myWriter.Close()


        End If




        Dim thePathx As String = Application.StartupPath & "\" & frmLogin.txtUsername.Text.ToLower & "_ads.dat"
        If frmAutoTweet.ListView1.Items.Count > 0 Then
            Dim myWriter As New IO.StreamWriter(thePathx)
            For Each myItem As ListViewItem In frmAutoTweet.ListView1.Items
                With myItem
                    myWriter.WriteLine(myItem.Text & "|_SP_|" & myItem.SubItems(1).Text)
                    '& "|" & myItem.SubItems(2).Text
                End With

            Next
            myWriter.Close()


        End If

        frmLogin.Dispose()
        frmPIN.Dispose()
        'frmSpamBlocker.Dispose()
        'frmAutoTweet.Dispose()
        NotifyIcon1.Visible = False
        End

    End Sub

    Private Sub LinkLabel2_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked

        On Error Resume Next

        frmAbout.StartPosition = FormStartPosition.CenterScreen
        frmAbout.Show()
        frmAbout.Focus()

    End Sub

    Private Sub LinkLabel1_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked

        System.Diagnostics.Process.Start("http://forum.smartpromoter.net/forumdisplay.php?f=4")

    End Sub

    Private Sub tmrFollowing_Tick(sender As System.Object, e As System.EventArgs) Handles tmrFollowing.Tick

        tmrFollowing.Enabled = False
        frmFollowing.Show()
        frmFollowing.ShowInTaskbar = True
        frmFollowing.WindowState = FormWindowState.Normal

    End Sub

    Private Sub tmrFollower_Tick(sender As System.Object, e As System.EventArgs) Handles tmrFollower.Tick

        tmrFollower.Enabled = False
        frmFollowers.Show()
        frmFollowers.ShowInTaskbar = True
        frmFollowers.WindowState = FormWindowState.Normal

    End Sub

    Private Sub tmrUpdate_Tick(sender As System.Object, e As System.EventArgs) Handles tmrUpdate.Tick

        On Error Resume Next

        tw.AuthenticateWith(consumer, consumersecret, My.Settings.m_strToken, My.Settings.m_strTokenSecret)

        If lblFollowers.Text > tw.AccountInformation.FollowersCount Then

            'we lost some followers

            Dim lostFollowers = lblFollowers.Text - tw.AccountInformation.FollowersCount

            lblPoN.Visible = True
            lblPoN.ForeColor = Color.Red
            lblPoN.Text = "↓" & lostFollowers
            'frmFollowers.lstFollowerCache.Items.Clear()
            'BackgroundWorker1.RunWorkerAsync()

        ElseIf lblFollowers.Text < tw.AccountInformation.FollowersCount Then

            'we gained some followers

            Dim gainedFollowers = tw.AccountInformation.FollowersCount - lblFollowers.Text

            lblPoN.Visible = True
            lblPoN.ForeColor = Color.Green
            lblPoN.Text = "↑" & gainedFollowers
            'frmFollowers.lstFollowerCache.Items.Clear()
            'BackgroundWorker1.RunWorkerAsync()

        End If

        lblFollowers.Text = tw.AccountInformation.FollowersCount
        lblFollowing.Text = tw.AccountInformation.FriendsCount
        lblTotalTweets.Text = tw.AccountInformation.StatusesCount
        lblVerified.Text = tw.AccountInformation.Verified


    End Sub

    Private Sub tmrSaveData_Tick(sender As System.Object, e As System.EventArgs) Handles tmrSaveData.Tick

        On Error Resume Next

        Dim i As Integer

        'sup mang

        Dim thePath As String = Application.StartupPath & "\" & frmLogin.txtUsername.Text.ToLower & "_spamblocker.dat"
        If frmSpamBlocker.lstBlacklist.Items.Count > 0 Then
            Dim myWriter As New IO.StreamWriter(thePath)

            For Each myItem As String In frmSpamBlocker.lstBlacklist.Items
                With myItem
                    myWriter.WriteLine(myItem)

                    ' & "|_SP_|" & myItem.SubItems(1).Text
                    '& "|" & myItem.SubItems(2).Text
                End With
            Next
            myWriter.Close()


        End If

        'Disabled from timer auto save thingy

        thePath = Application.StartupPath & "\" & frmLogin.txtUsername.Text.ToLower & "_ads.dat"
        If frmAutoTweet.ListView1.Items.Count > 0 Then
            Dim myWriter As New IO.StreamWriter(thePath)
            For Each myItem As ListViewItem In frmAutoTweet.ListView1.Items
                With myItem
                    myWriter.WriteLine(myItem.Text & "|_SP_|" & myItem.SubItems(1).Text)
                    '& "|" & myItem.SubItems(2).Text
                End With

            Next
            myWriter.Close()


        End If



    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        tw.AuthenticateWith(consumer, consumersecret, My.Settings.m_strToken, My.Settings.m_strTokenSecret)

        'If IO.File.Exists(Application.StartupPath & "\" & tw.AccountInformation.ScreenName.ToLower & "_follower-cache.dat").Equals(False) Then

        oWrite = IO.File.CreateText(Application.StartupPath & "\" & tw.AccountInformation.ScreenName.ToLower & "_follower-cache.dat")

        '

        For Each twUser As TwitterUser In tw.Followers

            oWrite.WriteLine(twUser.ScreenName & "," & twUser.ID)
            'frmFollowers.lstFollowerCache.Items.Add(twUser.ScreenName & "," & twUser.ID)

        Next

        oWrite.Close()

        'End If

        BackgroundWorker1.CancelAsync()

    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

        frmFollowers.Button3.Enabled = False

        frmFollowers.btnUpdateUnfollowers.Enabled = True

    End Sub
End Class